package com.ziyueyuwen.xiniao.controller.crm;

import com.suyeer.cache.RedisUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.service.UserService;
import com.ziyueyuwen.xiniao.util.TangLangUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.enums.ParamTypeEnum.ZH_PHONE_NUM;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * 对接螳螂项目
 *
 * @author jun 2019/3/12
 */
@Controller
@RequestMapping("/crm")
public class CRMController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/add_customer.do", method = RequestMethod.POST)
    public void addCustomer(@RequestParam(PARAM_CUSTOMER_PHONE_NUM) String customerPhoneNum,
                            @RequestParam(value = PARAM_SALES_PHONE_NUM, required = false) String salesPhoneNum,
                            @RequestParam(PARAM_CLASS_LEVEL) String classLevel,
                            @RequestParam(PARAM_CODE) String code) throws Exception {
        HttpResContent<Boolean> hrc = new HttpResContent<>("预约报名");
        // check.
        checkParam(ZH_PHONE_NUM, PARAM_CUSTOMER_PHONE_NUM, customerPhoneNum);
        checkParam(PARAM_CLASS_LEVEL, classLevel);
        checkParam(PARAM_CODE, code);
        // do.
        RedisUtil.checkCode(customerPhoneNum, code);
        ZyUser sales = userService.getByAccount(salesPhoneNum);
        String salesName = sales == null || !sales.getIfSysUser() ? "王塨" : sales.getUserName();
        String sourceType = "转介绍";
        if ("https://www.ziyueyuwen.com/".equals(hrc.getReferrer()) || hrc.getReferrer().contains("https://www.ziyueyuwen.com/?")) {
            sourceType = "官网";
        }
        TangLangUtil.addCustomer(customerPhoneNum, salesName, classLevel, sourceType);
        hrc.printLogAndResponse(true);
    }

}
